package de.iritgo.skillfull.component.motion;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;

import de.iritgo.skillfull.component.Component;
import de.iritgo.skillfull.component.MotionComponentImpl;
import de.iritgo.skillfull.component.visitor.Visitor;
import de.iritgo.skillfull.config.ConfigManager;
import de.iritgo.skillfull.entity.Entity;

public class RotationComponentImpl extends MotionComponentImpl implements RotationComponent
{
	private float rotation;

	private int clockwise;

	private float rotateSpeed;

	private ConfigManager configManager;
	
	public void setConfigManager(ConfigManager configManager) 
	{
		this.configManager = configManager;
	}

	public void setRotation (float rotation)
	{
		this.rotation = rotation;
	}
	
	public float getRotation ()
	{
		return rotation;
	}
	
	public float getRotateSpeed() 
	{
		return rotateSpeed;
	}

	public void setRotateSpeed(float rotateSpeed) 
	{
		this.rotateSpeed = rotateSpeed;
	}
	
	@Override
	public void accept (Visitor visitor)
	{
		visitor.visit (this);
		
		if (components == null)
			return;

		for (Component component : components)
		{
			component.accept (visitor);
		}
	}

	public void render (GameContainer container, Graphics g, Entity entity) 
	{
		if (configManager.functionActive("showEntityRotation"))
		{
			g.drawString ("R:" + rotation, x + 32, y);
		}
	}

	@Override
	public void printConfiguration ()
	{
		System.out.println ("Rotation: " + rotation + " :" + toString ());
	}
	
	public void setClockwise (boolean clockwise)
	{
		this.clockwise = clockwise ? 1 : -1; 
	}

	public void rotate (int delta)
	{
		rotation =  rotation + (rotateSpeed * delta * clockwise);
		if (rotation > 360)
			rotation = 0;
		if (rotation < 0)
			rotation = 360;
	}
}
